package com.ambow.servlet;

import java.io.IOException;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.Entity.Supplier;
import com.ambow.dao.BookDao;
import com.ambow.dao.Supplier1Dao;
import com.ambow.dao.SupplierDao;
@WebServlet("/Supplier_showServlet")
public class Supplier_showServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Supplier_showServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String ud=request.getParameter("update");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");  
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String info=request.getParameter("val");
		byte [] b=info.getBytes("iso-8859-1");
		info=new String(b,"utf-8");
		
		if(info.equals("personinfo")){//把从person_info.jsp页面传来的val和person相比较
			response.sendRedirect("pages/module/person/person001/user_detail.jsp");
		}
		else if(info.equals("allsupplier_query")){
			
			Supplier1Dao sp = new Supplier1Dao();
			
			List<Supplier> suppliers =  sp.allsupplierQuery();
			request.setAttribute("suppliers", suppliers);
			
			System.out.println(suppliers.get(0).getAddress());
			request.getRequestDispatcher("/pages/module/book/book002/supplier_list.jsp")
	          .forward(request, response);
			
			
	//		List list = operat.roleQuery("库存管理员");
//			Operator operator = (Operator) list.get(0);
			
	//		request.setAttribute("TextBookinfo", list);
	//		request.getRequestDispatcher("static/module/system/system002/user_list.jsp").forward(request, response);
//			System.out.println(aops.getName());
//			response.sendRedirect("static/module/system/system002/user_list.jsp");
			
		}
		else if(info.equals("supplier_query")){
			String name = request.getParameter("name");

				if(name==null)
					name = "%%";
				else
					name = "%"+name+"%";
				
				
				Supplier1Dao sp = new Supplier1Dao();
				
				List suppliers =  sp.supplierQuery(name);

				
				
				
				request.setAttribute("suppliers", suppliers);
				request.getRequestDispatcher("/pages/module/book/book002/supplier_list.jsp")
		          .forward(request, response);
				
			}
		else if(info.equals("delete")){
			String id=request.getParameter("id");
			int supplier_id=Integer.parseInt(id);
			
			Supplier1Dao sp = new Supplier1Dao();
			boolean f=sp.deleteSupplier(supplier_id);
			if(f=true){
				out.print("已经删除！");
			}
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
